// my/my.js
import {
  AccountModel
} from '../../../models/account'
import {
  PayModel
} from '../../../models/pay'
import {
  Format
} from '../../../utils/common'
const payModel = new PayModel()
const accountmodel = new AccountModel()
const app = getApp()
let last
const format = new Format()
Page({

  /**
   * 页面的初始数据
   */
  props: { // 组件传参默认值
    className: '', // 父级加入className
    verifyCodeLength: 6, // 验证码长度
    mobile: '', // 父传的手机号
    numberCode: true, // 是否是是数字键盘
    codeTime: 60, // 倒计时时间
    disabled: false, // 手机号是否可以编辑
    onSendCode: () => { }, // 点击发送验证码
    onCodeInput: () => { } // 输入验证码事件
  },
  data: {
    setNewpaw: '',
    setNewpawConfirm: '',
    btnDisabled: true, //提交按钮是否禁用
    btnName: '发送验证码', // 按钮显示名称
    canSend: false, // 是否可以发送验证吗
    hasSend: false, // 是否已经发送
    mobileValue: '', // 组件手机号的值
    mobileSafe: '', // 脱敏后的值
    txt: '', // 文本赋值
    mobileDisabled: false, // 手机号是否禁止输入
    code: '',
    phone: '',
    oldPhone: '',
    openTipPop: false, // 是否显示弹窗
    TipTxt: '', // 弹窗内容
    tips: '', // 点击按钮方法
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    if (app.globalData.user.phone) {
      let mobilePhone = (app.globalData.user.phone).substr(0,3)+"****"+(app.globalData.user.phone).substr(7);
      this.setData({
          phone: mobilePhone,
          oldPhone: app.globalData.user.phone,
          canSend: true
      })

    }
  },
  bindKeyInput(e) {
    console.log(e)
    var reg=/^\d+$/;
    if (reg.test(e.detail.value) ) {
      if (e.detail.value.length == 6) {
        this.data[e.currentTarget.dataset.name] = e.detail.value
      } else {
        this.data[e.currentTarget.dataset.name] = ''
        this.setData({
          btnDisabled: true,
        })
        return
      }
    } else {
      this.setData({
        btnDisabled: true,
      })
      my.showToast({
        type: 'none',
        content: '请输入数字密码',
        duration: 1000,
      });
      return
    }
    console.log(this.data[e.currentTarget.dataset.name])
    if (this.data.setNewpaw && this.data.setNewpawConfirm && this.data.code && this.data.idno) {
      this.setData({
        btnDisabled: false,
      })
    } else {
      this.setData({
        btnDisabled: true,
      })
    }
  },
  /**
   * 点击获取验证码触发
   * @method onSendCode
  */
  onSendCode() {
    let vm = this
    if (this.data.canSend) {
      this.settimer();
      this.setData({
        hasSend: true,
        txt: '已',
        canSend: false
      });
      accountmodel.mobileVerfiCode(this.data.oldPhone).then(res => {
        console.log(res)
        if (res) {
          if (res && res.code == '00') {
            vm.settimer();
            vm.setData({
              hasSend: true,
              txt: '已',
              canSend: false
            });
          } else if (res.message) {
            vm.setData({
              openTipPop: true,
              TipTxt: res.message,
              tips: 'close',
            });
          }
        } else {
          vm.setData({
            openTipPop: true,
            TipTxt: '获取验证码失败',
            tips: 'close',
          });
        }
      })
    } else {
      const {
        popoverShow,
        hasSend
      } = this.data;
      if (!hasSend && !popoverShow) {
        my.showToast({
          type: 'none',
          content: '获取手机号失败',
          duration: 1000,
        });
      }
    }
  },
  /**
   * 输入验证码触发事件并传给父
   * @method onCodeInput
   * @param {*} e
   */
  onCodeInput(e) {
    console.log(e)
    if (e.detail.value.length == 6) {
      this.setData({
        code: e.detail.value,
      })
      if (this.data.setNewpaw && this.data.setNewpawConfirm && this.data.idno) {
        this.setData({
          btnDisabled: false,
        })
      } else {
        this.setData({
          btnDisabled: true,
        })
      }
    } else {
      this.setData({
        btnDisabled: true,
        code: '',
      })
    }
    // this.props.onCodeInput && this.props.onCodeInput(e);
  },
  /**
   * 倒计时定时器
   * @method settimer
   */
  settimer() {
    let timer;
    let codeTime = this.props.codeTime;
    const {
      disabled
    } = this.props;
    clearInterval(timer);
    timer = setInterval(() => {
      codeTime--;
      this.setData({
        btnName: `倒计时${codeTime}秒`
      });
      if (codeTime === 0) {
        clearInterval(timer);
        const box = this.data.othervalue;
        this.setData({
          btnName: '重新发送',
          mobileDisabled: disabled ? true : false,
          canSend: true,
          txt: ''
        });
      }
    }, 1000);
  },
  clickBtn () {
    this.getsetgetChangePawPaw()
  },
  getsetgetChangePawPaw() {
    let vm = this
    if (this.data.setNewpaw != this.data.setNewpawConfirm) {
      my.showToast({
        type: 'none',
        content: '新密码两次输入不一致',
        duration: 1000,
      });
      return
    }
    accountmodel.resetPayPin(this.data.setNewpawConfirm,this.data.code,this.data.idno).then(res => {
      console.log(res)
      if (res) {
        if (res.message) {
          vm.setData({
            openTipPop: true,
            TipTxt: '重置支付密码成功',
            tips: 'href',
          });
        } else if (res.message) {
          vm.setData({
            openTipPop: true,
            TipTxt: res.message,
            tips: 'close',
          });
        }
      } else {
        vm.setData({
          openTipPop: true,
          TipTxt: '修改密码失败',
          tips: 'close',
        });
      }
    })
  },
  makePhoneCall() {
    my.makePhoneCall({ number: '400-010-3218' });
  },
  // 点击知道了
  tipPopSure(tips) {
    this.setData({
      openTipPop: false
    });
    if (tips == 'href') {
      my.navigateBack('1')
    }
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    
    // this.loadMyAccount()
  },
  
  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})